local extension = Package:new("jie")
extension.extensionName = "king"
local U = require "packages/utility/utility"


Fk:loadTranslationTable{
    ["jie"] = "界",
  }



local jie__zhangxiu = General:new(extension,"jiezhangxiu","qun",4)
  local jiexiongluan = fk.CreateActiveSkill{
    name = "jiexiongluan",
    anim_type = "offensive",
    target_num = 1,
    can_use = function(self, player)
      return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and
        #player:getAvailableEquipSlots() > 1
    end,
    card_filter = Util.FalseFunc,
    target_filter = function(self, to_select, selected, selected_cards)
      return Self.id ~= to_select
      end,
    on_use = function(self, room, effect,data,player)
      local to = room:getPlayerById(effect.tos[1])
      local player = room:getPlayerById(effect.from)
      local choice = room:askForChoices(player, player:getAvailableEquipSlots(), 2, 4, self.name, "#jiexiongluan-abort", false)
      room:abortPlayerArea(player, choice)
      local n = #choice
      player:drawCards(n, self.name)
      room:addPlayerMark(to, "@jiexiongluan-turn",n-1)    
  
      local targetRecorded = type(player:getMark("jiexiongluan_target-turn")) == "table" and player:getMark("jiexiongluan_target-turn") or {}
      table.insertIfNeed(targetRecorded, to.id)
      room:setPlayerMark(player, "jiexiongluan_target-turn", targetRecorded)
      if n == 4 then 
        room:handleAddLoseSkills(player, "wansha", nil, true, false)
      end
    end,
  }
  local jiexiongluan_prohibit = fk.CreateProhibitSkill{
    name = "#jiexiongluan_prohibit",
    prohibit_use = function(self, player, card)
      if player:getMark("@jiexiongluan-turn") > 0 then
        local subcards = card:isVirtual() and card.subcards or {card.id}
        return #subcards > 0 and table.every(subcards, function(id)
          return table.contains(player:getCardIds(Player.Hand), id)
        end)
    end
        
    end,
    prohibit_response = function(self, player, card)
      if player:getMark("@jiexiongluan-turn") > 0 then
        local subcards = card:isVirtual() and card.subcards or {card.id}
        return #subcards > 0 and table.every(subcards, function(id)
          return table.contains(player:getCardIds(Player.Hand), id)
        end)
      end
    end,
  }
  local jiexiongluan_targetmod = fk.CreateTargetModSkill{
    name = "#jiexiongluan_targetmod",
    bypass_times = function(self, player, skill, scope, card, to)
      if to:getMark("@jiexiongluan-turn") > 1 then
      if card and to then
        local targetRecorded = player:getMark("jiexiongluan_target-turn")
        return type(targetRecorded) == "table" and table.contains(targetRecorded, to.id)
      end
    end
    end,
    bypass_distances = function(self, player, skill, card, to)
      if to:getMark("@jiexiongluan-turn") > 1 then
      if card and to then
        local targetRecorded = player:getMark("jiexiongluan_target-turn")
        return type(targetRecorded) == "table" and table.contains(targetRecorded, to.id)
      end
    end
    end,
  }
  jiexiongluan:addRelatedSkill(jiexiongluan_targetmod)
  jiexiongluan:addRelatedSkill(jiexiongluan_prohibit)
  local jiecongjian = fk.CreateTriggerSkill{
   name = "jiecongjian",
    anim_type = "defensive",
    events = {fk.TargetConfirmed},
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) 
      --and data.card.type == Card.TypeTrick and #AimGroup:getAllTargets(data.tos) > 1 and not player:isNude()
    end,
    on_cost = function(self, event, target, player, data)
      local room = player.room
      local targets = table.map(
        table.filter(room.alive_players, function(p)
          return (not p:hasSkill("os__xiongjun"))
        end),
        Util.IdMapper
      )
      table.removeOne(targets, player.id)
      local tos, cardId = room:askForChooseCardAndPlayers(player, targets, 1, 1, nil, "#jiecongjian-give", self.name, true)
      if #tos > 0 then
        self.cost_data = {tos = tos, cards = {cardId}}
        return true
      end
    end,
    on_use = function(self, event,target, player, data)
      local room = player.room
      if Fk:getCardById(self.cost_data.cards[1]).type == Card.TypeEquip then--装备
        room:obtainCard(self.cost_data.tos[1], self.cost_data.cards, true, fk.ReasonGive, player.id, self.name)
        player:drawCards(2, self.name)
        local all_slots = {"WeaponSlot", "ArmorSlot", "DefensiveRideSlot", "OffensiveRideSlot", "TreasureSlot"}
        local choices = {}
        for _, equip_slot in ipairs(all_slots) do
          if table.contains(player.sealedSlots, equip_slot) then
            table.insert(choices, equip_slot)
          end
        end
        if #choices > 0 then
          local choice = player.room:askForChoice(player, choices, self.name, "#jiecongjian-choice", false)
          player.room:resumePlayerArea(player, {choice})
        end
      elseif Fk:getCardById(self.cost_data.cards[1]).type == Card.TypeTrick and data.card.type == Card.TypeTrick  then
        room:obtainCard(self.cost_data.tos[1], self.cost_data.cards, true, fk.ReasonGive, player.id, self.name)
        player:drawCards(2, self.name)--锦囊
      elseif Fk:getCardById(self.cost_data.cards[1]).type == Card.TypeBasic and data.card.type == Card.TypeBasic  then
        room:obtainCard(self.cost_data.tos[1], self.cost_data.cards, true, fk.ReasonGive, player.id, self.name)
        player:drawCards(2, self.name)--基本
      else 
        room:obtainCard(self.cost_data.tos[1], self.cost_data.cards, true, fk.ReasonGive, player.id, self.name)
        player:drawCards(1, self.name)
      end
    end,
  }
  jie__zhangxiu:addRelatedSkill("wansha")
  jie__zhangxiu:addSkill(jiexiongluan)
  jie__zhangxiu:addSkill(jiecongjian)
  Fk:loadTranslationTable{
    ["jiezhangxiu"] = "界张绣",
    ["designer:jiezhangxiu"] = "妄生",
    ["jiexiongluan"] = "雄乱",
    [":jiexiongluan"] = "出牌阶段限一次，你可以废除你的X个装备区，然后指定一名其他角色,并摸X张牌，发动指定效果，直到回合结束。"
    .."X大于等于2时，则你对其使用牌时，其不能使用和打出手牌；X大于等于3时，则你对其使用牌无距离和次数限制；"
    .."X为4时，则你本回合获得技能【完杀】。（X为你废除的装备栏）",
    ["#jiexiongluan-abort"] = "雄乱：请选择要废除的两个到四个装备栏",
    ["jiecongjian"] = "从谏",
    [":jiecongjian"] = "当你成为牌的目标后，你可以交给其他目标角色一张牌，然后摸一张牌，若给出的牌与指定你为目标的牌的类型一致或为装备牌，"
    .."则你额外摸一张牌，且若你给出的是装备牌，则可选择恢复一个装备栏。",
    ["@jiexiongluan-turn"] = "雄乱",
    ["#jiecongjian-give"] = "从谏：你可将一张牌交给一名其他目标角色，然后摸一张牌。若与指定你为目标的牌的类型一致或为装备牌，改为摸两张",
    ["#jiecongjian-choice"] = "从谏：选择要恢复的装备栏",
    ["$jiexiongluan1"] = "北地枭雄，乱世不败！！",
    ["$jiexiongluan2"] = "雄据宛城，虽乱世可安！",
    ["$jiecongjian1"] = "听君谏言，去危亡，保宗祀!",
    ["$jiecongjian2"] = "从谏良计，可得自保！",
    ["~jiezhangxiu"] = "若失文和……吾将何归……",
}  

--[[local jie__xizhicai = General(extension, "ex__xizhicai", "wei", 4)
local updataXianfu = function (room, player, target)
  local mark = player:getTableMark("jie__xianfu")
  table.insertIfNeed(mark[2], target.id)
  room:setPlayerMark(player, "jie__xianfu", mark)
  local names = table.map(mark[2], function(pid) return Fk:translate(room:getPlayerById(pid).general) end)
  room:setPlayerMark(player, "@jie__xianfu", table.concat(names, ","))
end
local jie__xianfu = fk.CreateTriggerSkill{
  name = "jie__xianfu",
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, self.name)
    player:broadcastSkillInvoke(self.name, math.random(2))
    local tos = room:askForChoosePlayers(player, table.map(room:getOtherPlayers(player, false), Util.IdMapper), 1, 999, "#jie__xianfu-choose", self.name, false, true)
    local mark = player:getTableMark(self.name)
    if #mark == 0 then mark = {{},{}} end
    table.insertIfNeed(mark[1], tos[1])
    room:setPlayerMark(player, self.name, mark)
    player:drawCards(#tos, self.name)
  end,
}
local jie__xianfu_delay = fk.CreateTriggerSkill{
  name = "#jie__xianfu_delay",
  events = {fk.Damaged, fk.HpRecover,fk.Damage},
  frequency = Skill.Compulsory,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local mark = player:getTableMark("jie__xianfu")
    if not player.dead and not target.dead and #mark > 0 and table.contains(mark[1], target.id) then
      return event == fk.Damaged or player:isWounded()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    updataXianfu (room, player, target)
    room:sendLog{ type = "#SkillDelayInvoked", from = player.id, arg = "jie__xianfu", }
    if event == fk.Damaged then
      player:broadcastSkillInvoke("jie__xianfu", math.random(2)+2)
      room:damage{
        to = player,
        damage = data.damage,
        skillName = "jie__xianfu",
      }
    else
      player:broadcastSkillInvoke("jie__xianfu", math.random(2)+4)
      if player:isWounded() then
        room:recover{
          who = player,
          num = data.num or data.damage ,
          recoverBy = player,
          skillName = "jie__xianfu",
        }
      end
    end
  end,
}
  local jie__chouce = fk.CreateTriggerSkill{
    name = "jie__chouce",
    anim_type = "masochism",
    events = {fk.Damaged,fk.HpRecover},
    on_trigger = function(self, event, target, player, data)
      self.cancel_cost = false
      if event == fk.Damaged  then
      for i = 1, data.damage  do
        if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
        self:doCost(event, target, player, data)
      end
      elseif event == fk.HpRecover then
      for i = 1, data.recover   do--
        if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
        self:doCost(event, target, player, data)
      end
    end
    end,
    on_cost = function(self, event, target, player, data)
      if player.room:askForSkillInvoke(player, self.name, data) then
        return true
      end
      self.cancel_cost = true
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|.|^nosuit",
      }
      room:judge(judge)
      if player.dead then return false end
      if judge.card.color == Card.Red then
        local targets = table.map(room.alive_players, Util.IdMapper)
        local tos = room:askForChoosePlayers(player, targets, 1, 1, "#ex__chouce-draw", self.name, false)
        local to = room:getPlayerById(tos[1])
        local mark = player:getTableMark("@@ex__xianfu")
        if to == mark then
        to:drawCards(2, self.name)
        player:drawCards(2, self.name)
        else
          to:drawCards(1, self.name)
          player:drawCards(1, self.name)
        end
      elseif judge.card.color == Card.Black then
        local targets = table.map(table.filter(room.alive_players, function(p) return not p:isAllNude() end), Util.IdMapper)
        if #targets == 0 then return end
        local tos = room:askForChoosePlayers(player, targets, 1, 1, "#ex__chouce-discard", self.name, false)
        local to = room:getPlayerById(tos[1])
        local card = room:askForCardChosen(player, to, "hej", self.name)
        room:throwCard({card}, self.name, to, player)
        player:drawCards(1, self.name)
      end
    end,
  }

  jie__xizhicai:addSkill("tiandu")
  jie__xizhicai:addSkill(jie__xianfu)
  jie__xizhicai:addSkill(jie__chouce)
  Fk:loadTranslationTable{
    ["ex__xizhicai"] = "界戏志才/有bug",
    ["ex__xianfu"] = "先辅",
    ["@@ex__xianfu"] = "先辅",
    [":ex__xianfu"] = "锁定技，游戏开始时，你选择至少1名其他角色，当其受到伤害后，你受到等量的伤害；当其造成伤害或回复体力后，你回复等量的体力。",
    ["jie__chouce"] = "筹策",
    [":jie__chouce"] = "当你受到1点伤害或恢复一点体力后，你可以进行判定，若结果为：黑色，你摸一张且你弃置一名角色区域里的一张牌；红色，你摸两张且你令一名角色摸一张牌（先辅的角色摸两张）。",
    ["#ex__xianfu-choose"] = "先辅: 请选择要先辅的角色",
    ["#jie__chouce-draw"] = "筹策: 令一名角色摸一张牌（若为先辅角色则摸两张）",
    ["#jie__chouce-discard"] = "筹策: 弃置一名角色区域里的一张牌",
    ["#SkillDelayInvoked"] = "%from 的“%arg”的延迟效果被触发",
  }]]


local jie__zhangchunhua = General(extension, "jie__zhangchunhua", "wei", 3, 5, General.Female)
  local jie__jueqing = fk.CreateTriggerSkill{
    name = "jie__jueqing",
    anim_type = "offensive",
    frequency = Skill.Compulsory,
    events = {fk.PreDamage},
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self)
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      room:changeMaxHp(data.to, -data.damage, self.name)
      room:changeMaxHp(player, data.damage, self.name)
      return true
    end,
  }
  jie__zhangchunhua:addSkill(jie__jueqing)
  jie__zhangchunhua:addSkill("shangshi")
  jie__zhangchunhua:addSkill("jianmie")
  Fk:loadTranslationTable{
    ["jie__zhangchunhua"] = "界张春华",
    ["jie__jueqing"] = "绝情",
    [":jie__jueqing"] = "锁定技，你造成的伤害均视为扣除体力上限，然后你获得等量的体力上限。",
  
}
  
  
local jie__weiyan = General(extension, "jie__weiyan", "shu", 4)
local jie__kuanggu = fk.CreateTriggerSkill{
  name = "jie__kuanggu",
  anim_type = "drawcard",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player 
  end,
  on_trigger = function(self, event, target, player, data)
    for i = 1, data.damage do
      if i > 1 and (self.cost_data == "Cancel" or not player:hasSkill(self)) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {"draw1", "Cancel"}
    if player:isWounded() then
      table.insert(choices, 2, "recover")
    end
    self.cost_data = room:askForChoice(player, choices, self.name)
    return self.cost_data ~= "Cancel"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if self.cost_data == "recover" then
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name
      })
    elseif self.cost_data == "draw1" then
      player:drawCards(1, self.name)
    end
    room:addPlayerMark(player, "@jie__kuanggu",1)
  end,
}
local jie__kuanggu_DamageInflicted = fk.CreateTriggerSkill{
  name = "#jie__kuanggu_DamageInflicted",
  anim_type = "drawcard",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
     return  player:getMark("@jie__kuanggu") >= data.damage and player:hasSkill(self) and target == player 
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@jie__kuanggu",data.damage)
    return true
  end,
}
local jie__qimou = fk.CreateViewAsSkill{
  name = "jie__qimou",
  prompt = "#jie__qimou",
  interaction = function()
    local names, all_names = {} , {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card:isCommonTrick() and not card.is_derived and not table.contains(all_names, card.name)  then
        table.insert(all_names, card.name)
        local to_use = Fk:cloneCard(card.name)
        if Self:canUse(to_use) and not Self:prohibitUse(to_use) then
          table.insert(names, card.name)
        end
      end
    end
    if #names > 0 then
    return UI.ComboBox {choices = names, all_choices = all_names}
    end
  end,
  enabled_at_play = function(self, player)
    return  player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and player:getMark("@jie__kuanggu") > 0 and 
      table.find(Fk:currentRoom().alive_players, function(p)
        return table.find({ p.general, p.deputyGeneral }, function(name) return player.maxHp > 1 end)
      end)
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,

  before_use = function(self, player, use)
    local room = player.room
    if player:getMark("@jie__kuanggu") > 0 then
      room:setPlayerMark(player, "@jie__kuanggu", 0)
    end
  end,
}
jie__kuanggu:addRelatedSkill(jie__kuanggu_DamageInflicted)
jie__weiyan:addSkill(jie__kuanggu)
jie__weiyan:addSkill(jie__qimou)
Fk:loadTranslationTable{
  ["jie__weiyan"] = "界魏延",
  ["jie__kuanggu"] = "狂骨",
  [":jie__kuanggu"] = "你造成1点伤害后，你可以选择摸一张牌或回复1点体力，然后获得一枚“骨”标记，当你受到伤害时，若你的“骨”的数量不小于伤害值，则可以弃置伤害值的“骨”并防止此伤害。",
  ["jie__qimou"] = "奇谋",
  [":jie__qimou"] = "出牌阶段限一次，你可以移去所有的“骨”并视为使用一张锦囊牌",
  ["@jie__kuanggu"] = "骨",
  ["jie__kuanggu_DamageInflicted"] = "狂骨"
}

--[[local jie__sunquan = General(extension, "jie__sunquan", "wu", 4)





Fk:loadTranslationTable{
  ["jie__sunquan"] = "孙权",

  ["jie__zhiheng"] = "制衡",
  [":jie__zhiheng"] = "出牌阶段限一次，你可以选择一至两名其他角色，然后若其手牌数不为最少，则其弃置至最少，然后你获得弃置的牌，"..
  "若选择的角色手牌数大于你，则弃置与你手牌数相等，然后你获得其弃置的牌。",

  ["#yin__pingjian-active"] = "发动 评荐，从三个出牌阶段的技能中选择一个获得",
  ["#yin__pingjian-choice"] = "评荐：选择要获得的技能",

}]]


local jie__zhangliang = General(extension, "jie__zhangliang", "qun", 4)

local moujijun = fk.CreateTriggerSkill{
  name = "moujijun",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUsing, fk.CardResponding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:getMark("@mou_jun") < 184 and data.card.number > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n1 = tostring(player:getMark("@mou_jun"))
    room:addPlayerMark(player, "@mou_jun", math.min(data.card.number, 184 - player:getMark("@mou_jun")))
    local n2 = tostring(player:getMark("@mou_jun"))
    if #n1 == 1 then
      if #n2 == 1 then return end
    else
      if n1:sub(#n1 - 1, #n1 - 1) == n2:sub(#n2 - 1, #n2 - 1) then return end
    end
    local x = n2:sub(#n2 - 1, #n2 - 1)
    if x == 0 then x = 10 end  --yes, tenyear is so strange
    local card = room:getCardsFromPileByRule(".|"..x)
    if #card > 0 then
      room:moveCards({
        ids = card,
        to = player.id,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player.id,
        skillName = self.name,
      })
    end
  end,

  on_lose = function (self, player)
    player.room:setPlayerMark(player, "@mou_jun", 0)
  end,
}

local mou__fangtong = fk.CreateActiveSkill{
  name = "mou__fangtong",
  anim_type = "offensive",
  prompt = "#mou__fangtong",
  can_use = function(self, player)
    return player:getMark("@mou_jun") >= 36
  end,
  card_num = 0,
  card_filter = function() return false end,
  target_num = 1,
  target_filter = function(self, to_select, selected) 
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    room:removePlayerMark(player, "@mou_jun", 36)
    room:damage { from = player, to = to, damage = 3, skillName = self.name, damageType = fk.ThunderDamage }
  end,
}

jie__zhangliang:addSkill(mou__fangtong)

jie__zhangliang:addSkill(moujijun)

Fk:loadTranslationTable{
  ["jie__zhangliang"] = "界张梁",
  ["@mou_jun"] = "军",
  ["mou__fangtong"] = "方统",
  [":mou__fangtong"] = "出牌阶段，你可以移去36点“军”标记，对一名其他角色造成3点雷电伤害。",
  ["#mou__fangtong"] = "方统：移去36个“道兵”，对一名其他角色造成3点雷电伤害！",
  ["moujijun"] = "集军",
  [":moujijun"] = "锁定技，当你使用或打出一张牌后，获得等同于此牌点数的“军”标记，然后若“军”标记数的十位数变化，你随机获得牌堆中一张点数为变化后十位数的牌。",
  ["designer:mouzhangliang"] = "妄生",
}




--[[

local jie__maojie = General(extension, "jie__maojie", "wei", 3)
local jie__bingqing = fk.CreateTriggerSkill{
  name = "jie__bingqing",
  events = {fk.CardUseFinished},
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) 
  end,
  on_cost = function(self, event, target, player, data)
    local suit1 =
    local suit2 =
    local suit3 =
    local suit4 =
    self.cost_data = {suit1, suit2, suit3, suit4}
    if data.card.suit ~= Card.NoSuit then
      suit1 = suit2
      suit2 = suit3
      suit3 = suit4
      suit4 = data.card.suit
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    if suits == 2 then

    elseif suitsNum == 3 then

    elseif 
      room:damage({
        from = player,
        to = to,
        damage = 1,
        damageType = fk.NormalDamage,
        skillName = self.name,
      })
    end
  end,

  refresh_events = {fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self, true) and target == player and
      player.phase == Player.Play and
      data.card.suit ~= Card.NoSuit and
      (type(player:getMark("@bingqing-phase")) ~= "table" or
      not table.contains(player:getMark("@bingqing-phase"), "log_" .. data.card:getSuitString()))
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local typesRecorded = type(player:getMark("@bingqing-phase")) == "table" and player:getMark("@bingqing-phase") or {}
    table.insert(typesRecorded, "log_" .. data.card:getSuitString())
    room:setPlayerMark(player, "@bingqing-phase", typesRecorded)

    data.extra_data = data.extra_data or {}
    data.extra_data.firstCardSuitUseFinished = true
  end,
}
jie__maojie:addSkill(jie__bingqing)
Fk:loadTranslationTable{
  ["jie__maojie"] = "界毛玠",
  ["jie__bingqing"] = "秉清",
  [":jie__bingqing"] = "当你使用牌结算结束后，若你连续使用两张不同花色的牌时，你可以弃置一名角色区域内一张牌；"..
  "若你连续使用三张不同花色的牌时，你可以令一名角色摸两张牌；若你连续使用四张不同花色的牌时，你可以对一名角色造成1点伤害。",
  ["@jie__bingqing"] = "秉清",
  ["#jie__bingqing-draw"] = "秉清：你可以令一名角色摸两张牌",
  ["#jie__bingqing-discard"] = "秉清：你可以弃置一名角色区域里的一张牌",
  ["#jie__bingqing-damage"] = "秉清：你可以对一名其他角色造成1点伤害",

}]]


local jie__wenyang = General(extension, "jie__wenyang", "wei", 4)
jie__wenyang.subkingdom = "wu"

local jie__wenyang_win = fk.CreateActiveSkill{ name = "jie__wenyang_win_audio" }
jie__wenyang_win.package = extension
Fk:addSkill(jie__wenyang_win)
Fk:loadTranslationTable{
  ["jie__wenyang"] = "文鸯",
  ["~jie__wenyang"] = "洛阳宫前屈枉冢，乐嘉城上凄然风……",
  ["$jie__wenyang_win_audio"] = "秦凉既平，吾定名震天下！",
}
local jie__quedi = fk.CreateTriggerSkill{
  name = "jie__quedi",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(self) and
      player:usedSkillTimes(self.name, Player.HistoryTurn) < (2 + player:getMark("jie__choujue_buff-turn")) and
      table.contains({ "slash", "duel" }, data.card.trueName) and
      AimGroup:isOnlyTarget(player.room:getPlayerById(data.to), data) and
      player.room:getPlayerById(data.to):isAlive()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = {"beishui", "jie__quedi-offense","Cancel"}
    local to = room:getPlayerById(data.to)
    if not to:isKongcheng() then
      table.insert(choices, "jie__quedi-prey")
    end
    local prompt = "#jie__quedi-choice::"..data.to..":"..player:usedSkillTimes(self.name, Player.HistoryTurn)..":"
    ..(1 + player:getMark("jie__choujue_buff-turn"))
    local choice = room:askForChoice(player, choices, self.name, prompt, nil,
    {"beishui", "jie__quedi-prey", "jie__quedi-offense", "Cancel"})
    if choice ~= "Cancel" then
      self.cost_data = choice
      return true
    end

    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room

    local to = room:getPlayerById(data.to)
    if self.cost_data == "jie__quedi-prey" or (self.cost_data == "beishui" and not to:isAllNude()) then
      local cardId = room:askForCardChosen(player, to, "hej", self.name)
      room:obtainCard(player, cardId, false, fk.ReasonPrey)
      if player.dead then return false end
    end
    if self.cost_data == "jie__quedi-offense" or self.cost_data == "beishui" then
      if #room:askForDiscard(player, 1, 1, false, self.name, true, "." ,
        "#jie__quedi-discard:::" .. data.card.trueName) > 0 then
        data.additionalDamage = (data.additionalDamage or 0) + 1
        if player.dead then return false end
      end
    end

    if self.cost_data == "beishui" then
      room:changeMaxHp(player, -1)
    end
  end,
}
Fk:loadTranslationTable{
  ["jie__quedi"] = "却敌",
  [":jie__quedi"] = "每回合限两次，当你使用【杀】或【决斗】指定唯一目标后，你可以选择一项：1.获得其区域内一张牌；2.弃置一张牌，令此【杀】或【决斗】"..
  "伤害基数+1；背水：减1点体力上限。",
  ["jie__quedi-prey"] = "获得其区域内一张牌",
  ["jie__quedi-offense"] = "弃基本牌令此伤害+1",
  ["#jie__quedi-discard"] = "却敌：可以弃置1张牌，令此【%arg】伤害+1",
  ["#jie__quedi-choice"] = "你可以对 %dest 发动“却敌”",

  ["$jie__quedi1"] = "乐嘉风起残云散，万钧雷震万千军！",
  ["$jie__quedi2"] = "进袭如惊涛，破围如落潮！",

}
jie__wenyang:addSkill(jie__quedi)
local jie__chuifeng = fk.CreateViewAsSkill{
  name = "jie__chuifeng",
  prompt = function (self)
    return "#jie__chuifeng-prompt:::"
  end,
  enabled_at_play = function(self, player)
    return  player.hp > 0 
  end,
  card_filter = function()
    return false
  end,
  view_as = function(self, cards)
    local card = Fk:cloneCard("duel")
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player)
    player.room:loseHp(player, 1, self.name)
  end,
  after_use = function(self, player)
    local room = player.room
    room:changeKingdom(player, "wu", true)
    room:handleAddLoseSkills(player, "-jie__chuifeng", nil)
    room:handleAddLoseSkills(player, "jie__chongjian", nil)

  end,
}
local jie__chuifengDefence = fk.CreateTriggerSkill{
  name = "#jie__chuifeng_defence",
  anim_type = "defensive",
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jie__chuifeng) and data.card and table.contains(data.card.skillNames, jie__chuifeng.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    return true
  end,
}
jie__chuifeng:addAttachedKingdom("wei")
jie__chuifeng:addRelatedSkill(jie__chuifengDefence)
Fk:loadTranslationTable{
  ["jie__chuifeng"] = "椎锋",
  ["#jie__chuifeng_defence"] = "椎锋",
  [":jie__chuifeng"] = "魏势力技，你可以失去1点体力，并视为使用一张【决斗】,当你受到以此法使用的【决斗】造成的伤害时，防止此伤害；最后变更势力为【吴】，失去技能【椎锋】，获得技能【冲坚】。",
  ["#jie__chuifeng-prompt"] = "椎锋：失去1点体力，并视为使用一张【决斗】",
  
  ["$jie__chuifeng1"] = "淮南讨逆之役，次骞愿为先锋！",
  ["$jie__chuifeng2"] = "吾枪之利？汝一试便知！",
}
jie__wenyang:addSkill(jie__chuifeng)
local jie__chongjian = fk.CreateViewAsSkill{
  name = "jie__chongjian",
  prompt = "#jie__chongjian-prompt",
  interaction = function()
    local names = {}
    for name, _ in pairs(Fk.all_card_types) do
      local card = Fk:cloneCard(name)
      card.skillName = "jie__chongjian"
      if not card.is_derived and not table.contains(Fk:currentRoom().disabled_packs, card.package.name)
        and (card.name == "analeptic" or card.trueName == "slash")
        and ((Fk.currentResponsePattern == nil and Self:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card))) then
        if table.insertIfNeed(names, name) then
          local to_use = Fk:cloneCard(card.name)
          to_use.skillName = "jie__chongjian"
        end
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  pattern = "slash,analeptic",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeEquip
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, useData)
    useData.extra_data = useData.extra_data or {}
    useData.extra_data.jie__chongjianUser = player.id
    useData.extraUse = true
  end,
  after_use = function(self, player)
    local room = player.room
    room:changeKingdom(player, "wei", true)
    room:handleAddLoseSkills(player, "-jie__chongjian", nil)
    room:handleAddLoseSkills(player, "jie__chuifeng", nil)
  end,
  enabled_at_response = function(self, player, cardResponsing)
    return player:hasSkill(self) and not cardResponsing
  end,
}
jie__chongjian:addAttachedKingdom("wu")
jie__wenyang:addSkill(jie__chongjian)
Fk:loadTranslationTable{
  ["jie__chongjian"] = "冲坚",
  [":jie__chongjian"] = "吴势力技，你可以将一张装备牌当无次数限制的【酒】或无距离次数限制且无视防具的任意一种【杀】使用。当你以此法使用的【杀】对一名角色造成伤害后，"..
  "你获得其区域里的X张牌（X为伤害值）；最后变更势力为【魏】，失去技能【冲坚】，获得技能【椎锋】。",
  ["#jie__chongjian_buff"] = "冲坚",
  ["#jie__chongjian-prompt"] = "冲坚：将装备牌当【酒】或无距离次数限制且无视防具的任意一种【杀】使用",

  ["$jie__chongjian1"] = "奋六钧之力，破九天重云！",
  ["$jie__chongjian2"] = "攻敌营垒，不留一隅之地！",
}
local jie__chongjianBuff = fk.CreateTriggerSkill{
  name = "#jie__chongjian_buff",
  mute = true,
  events = {fk.Damage},
  can_trigger = function (self, event, target, player, data)
    if not (player.dead or data.to.dead) and #data.to:getCardIds(Player.Equip) > 0 then
      local parentUseData = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      return parentUseData and (parentUseData.data[1].extra_data or {}).jie__chongjianUser == player.id
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local equipsNum = #data.to:getCardIds(Player.Equip)
    local num = math.min(equipsNum, data.damage)
    local cards = room:askForCardsChosen(player, data.to, num, num, "hej", self.name)
    room:obtainCard(player, cards, true, fk.ReasonPrey, player.id, self.name)
  end,

  refresh_events = { fk.TargetSpecified }, --不会和吹风自选，refresh好了
  can_refresh = function (self, event, target, player, data)
    return not player.dead and (data.extra_data or {}).jie__chongjianUser == player.id
  end,
  on_refresh = function (self, event, target, player, data)
    local to = player.room:getPlayerById(data.to)
    if not to.dead then
      to:addQinggangTag(data)
    end
  end
}
jie__chongjian:addRelatedSkill(jie__chongjianBuff)
local jie__chongjianUnlimited = fk.CreateTargetModSkill{
  name = "#jie__chongjian_unlimited",
  --[[residue_func = function(self, player, skill, scope, card)
    return scope == Player.HistoryPhase and card and table.contains(card.skillNames, jie__chongjian.name) and 999 or 0
  end,]]
  bypass_times = function(self, player, skill, scope, card, to)
    return card and table.contains(card.skillNames, jie__chongjian.name)
  end,
  bypass_distances = function(self, player, skill, card)
    return card and table.contains(card.skillNames, jie__chongjian.name)
  end,
}
jie__chongjian:addRelatedSkill(jie__chongjianUnlimited)
local jie__choujue = fk.CreateTriggerSkill{
  name = "jie__choujue",
  anim_type = "drawcard",
  events = {fk.Deathed},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.damage and data.damage.from == player
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:changeMaxHp(player, 1)
    room:recover { num = 1, skillName = self.name, who = player , recoverBy = player}
    if player.dead then return false end
    room:drawCards(player, 2, self.name)
    room:addPlayerMark(player, "jie__choujue_buff-turn", 2)
  end,
}
Fk:loadTranslationTable{  
  ["jie__choujue"] = "仇决",
  [":jie__choujue"] = "锁定技，当你杀死一名角色后，你加1点体力上限,恢复一点体力，摸两张牌，你本回合〖却敌〗可发动次数+2。",
  ["$jie__choujue1"] = "国仇家怨，汝搓骨难偿！",
  ["$jie__choujue2"] = "彻天盈海之仇，纵身死亦不绝！",
}
jie__wenyang:addSkill(jie__choujue)

local jie__quyi = General(extension, "jie__quyi", "qun", 4)
local jie__fuji = fk.CreateTriggerSkill{
  name = "jie__fuji",
  anim_type = "offensive",
  events = {fk.CardUsing},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      (data.card.trueName == "slash" or data.card:isCommonTrick()) and
      table.find(player.room:getOtherPlayers(player, false), function(p) return player:inMyAttackRange(p)end)
  end,
  on_use = function(self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player, false), function(p) return player:inMyAttackRange(p) end)
    if #targets > 0 then
      data.disresponsiveList = data.disresponsiveList or {}
      for _, p in ipairs(targets) do
        table.insertIfNeed(data.disresponsiveList, p.id)
      end
    end
  end,
}
local jie__jiaozi = fk.CreateTriggerSkill{
  name = "jie__jiaozi",
  anim_type = "offensive",
  events = {fk.DamageCaused, fk.DamageInflicted},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return target == player and player:hasSkill(self) and data.to and data.to:getHandcardNum() < player:getHandcardNum()
    else  
      return target == player and player:hasSkill(self) and data.from and data.from:getHandcardNum() < player:getHandcardNum() 
    end
  end,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end,
}
jie__quyi:addSkill(jie__fuji)
jie__quyi:addSkill(jie__jiaozi)
Fk:loadTranslationTable{
  ["jie__quyi"] = "麴义",

  ["jie__fuji"] = "伏骑",
  [":jie__fuji"] = "锁定技，当你使用【杀】或普通锦囊牌时，你令所有你攻击范围内的角色不能响应此牌。",
  ["jie__jiaozi"] = "骄恣",
  [":jie__jiaozi"] = "锁定技，当你造成伤害/受到伤害时，若你的手牌数量大于等于目标/伤害来源，则此伤害+1。",
}



















return extension  